<template>
  <div class="employees-detail-container">
    <div class="app-container">
      <el-card>
        <el-tabs>
          <el-tab-pane label="登录账号设置">
            <!-- 放置表单 -->
            <el-form ref="userForm" label-width="120px" style="margin-left: 120px; margin-top:30px" :model="userInfo" :rules="rules">
              <el-form-item label="姓名:" prop="username">
                <el-input v-model="userInfo.username" style="width:300px" />
              </el-form-item>
              <el-form-item label="密码:" prop="newPassword2">
                <el-input v-model="userInfo.newPassword2" style="width:300px" type="password" />
              </el-form-item>
              <el-form-item>
                <el-button type="primary" @click="saveUser">更新</el-button>
              </el-form-item>
            </el-form>
          </el-tab-pane>

          <el-tab-pane label="个人详情">
            <el-row type="flex" justify="end">
              <el-tooltip class="item" effect="dark" content="打印基本个人信息" placement="bottom">
                <router-link :to="`/employees/print/${userid}?type=personal`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <user-info />
          </el-tab-pane>

          <!-- 岗位信息 -->
          <el-tab-pane label="岗位信息">
            <el-row type="flex" justify="end">
              <el-tooltip class="item" effect="dark" content="打印基本岗位信息" placement="bottom">
                <router-link :to="`/employees/print/${userid}?type=job`">
                  <i class="el-icon-printer" />
                </router-link>
              </el-tooltip>
            </el-row>
            <job-info />
          </el-tab-pane>
        </el-tabs>
      </el-card>
    </div>
  </div>
</template>

<script>
import { getUserDetailById } from '@/api/user' // 获取用户基本信息
import { SaveUserDetailById } from '@/api/employees' // 保存用户信息
import UserInfo from './compoents/user-info.vue' // 个人详情
import jobInfo from './compoents/job-info' // 岗位详情
export default {
  name: 'EmployeesDetail',
  components: {
    UserInfo, // 个人详情
    jobInfo // 岗位信息
  },
  data() {
    return {
      UserComponent: UserInfo, // 个人详情
      JobInfo: jobInfo, // 岗位信息
      userInfo: {
        username: '', // 用户名
        newPassword2: '' // 密码
      },
      rules: { // 规则对象
        username: [
          { required: true, message: '姓名不能为空', trigger: 'blur' }
        ],
        newPassword2: [
          { min: 6, max: 9, message: '密码必须是6-12位', trigger: ['blur'], required: true }
        ]
      }
    }
  },
  computed: {
    userid() { // 动态用户id
      return this.$route.params.id
    }
  },
  created() {
    this.getUserDetailById() // 获取当前的用户基本信息
  },
  methods: {
    async getUserDetailById() { // 获取当前的用户基本信息
      const res = await getUserDetailById(this.userid)
      this.userInfo = res
    },
    async saveUser() { // 保存更新用户信息
      await this.$refs.userForm.validate
      await SaveUserDetailById({ ...this.userInfo, password: this.userInfo.newPassword2 })
      this.$message.success('更新用户信息成功')
      this.userInfo = {
        username: '', // 用户名
        newPassword2: '' // 密码
      }
    }
  }
}
</script>
